package com.zd.commodity.productMng.biz.impl;

import java.io.File;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import utils.Util;

import com.zd.commodity.biz.impl.BaseBiz;
import com.zd.commodity.productMng.biz.IProductIntroductionBiz;
import com.zd.commodity.productMng.dao.IProductIntroductionDao;
import com.zd.commodity.productMng.entity.ProductIntroduction;
import com.zd.commodity.userMng.entity.User;
@Service
public class ProductIntroductionBiz extends BaseBiz<ProductIntroduction>
		implements IProductIntroductionBiz {
	private IProductIntroductionDao iProductIntroductionDao;

	public IProductIntroductionDao getiProductIntroductionDao() {
		return iProductIntroductionDao;
	}
	@Autowired
	public void setiProductIntroductionDao(
			IProductIntroductionDao iProductIntroductionDao) {
		this.iProductIntroductionDao = iProductIntroductionDao;
		this.setBaseDao(iProductIntroductionDao);
	}
	@Override
	public String updateByTxts(User user,String path) {
		String message;
		StringBuilder sb = new StringBuilder();//记录重名的详情
		String introductionPath = path.substring(0,path.lastIndexOf("\\")+1)+"upload/introduction";
		File file=new File(introductionPath);
		File[] files = file.listFiles();
		for (int i=0;i<files.length;i++) {
			//1.如果详情表中已存在名称相同的，则记录后跳过
			ProductIntroduction introduction = new ProductIntroduction();
			String fileName = files[i].getName();
			introduction.setName(fileName);
			List<ProductIntroduction> list = iProductIntroductionDao.getList(introduction, null, null);
			if(list.size()>0){
				sb.append(fileName+",");
				continue;
			}
			//2.文件名保存到详情名称，txt内容保存到详情内容
			introduction.setCreationTime(new Timestamp(System.currentTimeMillis()));
			introduction.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			introduction.setCreator(user);
			introduction.setUpdater(user);
			introduction.setName(fileName.substring(0,fileName.indexOf(".txt")));
			introduction.setContent(Util.readTxt(files[i]));
			files[i].delete();
			iProductIntroductionDao.add(introduction);
		}
		if(sb.length()==0){
			message="详情导入成功！";
		}else{
			message="以下文件名的详情已存在："+sb.toString();
		}
		return message;
	}

}
